package com.example.xymarket.adapter;

import android.content.Context;
import android.graphics.drawable.Drawable;
import android.os.Handler;
import android.os.Message;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

import com.example.xymarket.R;
import com.example.xymarket.httpUtil.AsyncImageLoader;
import com.example.xymarket.po.Merchant;
import com.example.xymarket.common.MyUrl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Random;

public class MerchantAdapter extends BaseAdapter {

    Context context;
    Handler contextHandler;
    public HashMap<Integer, Drawable> hm;
    List<Merchant> items;
    public MerchantAdapter(List<Merchant> items, Context context,  Handler handler)
    {
        hm=new HashMap<>();
        this.items=items;
        this.context=context;
        this.contextHandler=handler;
    }
    @Override
    public int getCount() {
        return items.size();
    }

    @Override
    public Object getItem(int position) {
        return items.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        try {
            Merchant merchant = items.get(position);
            String url = MyUrl.ImagesUrl + merchant.getImage();
            MerchantAdapter.MyViewHolder viewHolder = new MerchantAdapter.MyViewHolder();
            convertView = LayoutInflater.from(context).inflate(R.layout.item_merchant, null);
            viewHolder.id = merchant.getId();
            viewHolder.ownerId=merchant.getUserId();
            viewHolder.name = (TextView) convertView.findViewById(R.id.order_name);
            viewHolder.name.setText(merchant.getName());
            viewHolder.description = (TextView) convertView.findViewById(R.id.merchant_dis);
            viewHolder.description.setText(merchant.getDescription());
            viewHolder.phone = (TextView) convertView.findViewById(R.id.order_price);
            viewHolder.phone.setText("联系方式："+merchant.getPhone());
            viewHolder.distance = (TextView) convertView.findViewById(R.id.merchant_distance);
            viewHolder.distance.setText(getRandomDistance());
            viewHolder.imageView = (ImageView) convertView.findViewById(R.id.order_avatar);
            if (hm.get(position) != null) {
                viewHolder.imageView.setImageDrawable(hm.get(position));
            } else {
                AsyncImageLoader asyncImageLoader = new AsyncImageLoader();
                asyncImageLoader.loadImage(url, viewHolder.imageView,hm,position);
            }
            View.OnClickListener listener = new View.OnClickListener()
            {

                @Override
                public void onClick(View v) {
                    Map<String,Object> map=new HashMap<>();
                    merchant.setDrawable(viewHolder.imageView.getDrawable());
                    merchant.setDistance(viewHolder.distance.getText().toString());
                    Merchant.merchant=merchant;
                    Message message=new Message();
                    message.obj=map;
                    message.what=101;
                    contextHandler.sendMessage(message);
                }
            };
            viewHolder.go = (Button) convertView.findViewById(R.id.button4);
            viewHolder.go.setOnClickListener(listener);
            convertView.setOnClickListener(listener);
            convertView.setTag(viewHolder);
            return convertView;
        }
        catch (Exception e)
        {
            return null;
        }
    }

    public String getRandomDistance()
    {
        Random r = new Random();
        int num = r.nextInt(300);
        return String.valueOf(num/10)+"."+String.valueOf(num%10)+"Km";
    }
    public final class MyViewHolder {
        int ownerId;
        int id;
        TextView name;
        TextView distance;
        TextView description;
        TextView phone;
        ImageView imageView;
        Button go;
    }
}
